package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.queryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")EmployeeQueryObject qo){
        PageResult<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageResult",pageResult);

        List<Department> departments = departmentService.listAll();
         model.addAttribute("departments",departments);
        return "employee/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            if (id != null) {
                employeeService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.listAll();
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);
        model.addAttribute("departments",departments);

        if(id!=null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "/employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids) {
        if (employee.getId() != null) {
          employeeService.update(employee,ids);
        } else {
            employeeService.save(employee,ids);
        }
        return "redirect:/employee/list.do";
    }




    @RequestMapping("/updatePassword")
    public String updatePwd(){
        return "common/updatePwd";
    }



    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(String oldPassword, String newPassword, HttpSession session){
        Employee employee = (Employee) session.getAttribute("USER_IN_SESSION");
        try {
            employeeService.updatePwd(newPassword,oldPassword,employee.getId());
            session.invalidate();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }

    }


    @RequestMapping("/setInput")
    public String setInput(Long id,Model model){
        Employee employee = employeeService.get(id);
        model.addAttribute("employee",employee);
        return "/common/resetPwd";
    }


    @RequestMapping("/resetPwd")
    @ResponseBody
    public JsonResult update(String newPassword,Long id){

        try {
            employeeService.resetPwdById(newPassword, id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }

    }
}